package com.situ.web.dao.impl;

import com.situ.web.dao.IUserDao;
import com.situ.web.pojo.entity.User;
import com.situ.web.pojo.query.UserQuery;
import com.situ.web.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements IUserDao {
    @Override
    public List<User> selectByPage(int offset, Integer pageSize) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<User> list = new ArrayList<>();
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select id,name,password from users limit ?,?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, offset);
            statement.setInt(2, pageSize);
            System.out.println(statement);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String password = resultSet.getString("password");
                User user = new User(id, name, password);
                list.add(user);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return list;
    }

    @Override
    public int selectTotalCount() {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        int count = 0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select count(*) from users";
            statement = connection.prepareStatement(sql);
            System.out.println(statement);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                count = resultSet.getInt(1);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        };

        return count;
    }

    @Override
    public User login(String name, String password) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        User user = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select id,name,password from users where name=? and password=?";
            statement = connection.prepareStatement(sql);
            statement.setString(1, name);
            statement.setString(2, password);
            System.out.println(statement);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                //String name = resultSet.getString("name");
                //String password = resultSet.getString("password");
                user = new User(id, name, password);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return user;
    }

    @Override
    public List<User> selectByPage(UserQuery userQuery) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<User> list = new ArrayList<>();
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select id,name,password from banji where 1=1";
            //构造一个集合存放所有的条件
            List<Object> queryList = new ArrayList<>();
            String queryName = userQuery.getName();
            if (queryName != null && !queryName.equals("")) {
                sql += " and name like ?";
                queryList.add("%" + queryName + "%");
            }
            String queryPassword = userQuery.getPassword();
            if (queryPassword != null && !queryPassword.equals("")) {
                sql += " and address like ?";
                queryList.add("%" + queryPassword + "%");
            }
            sql += " limit ?";
            statement = connection.prepareStatement(sql);
            for (int i = 0; i < queryList.size(); i++) {
                statement.setObject(i + 1, queryList.get(i));
            }
            int offset = (userQuery.getPage() - 1) * userQuery.getLimit();
            statement.setInt(queryList.size() + 1, offset);
            statement.setInt(queryList.size() + 2, userQuery.getLimit());
            System.out.println(statement);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String password = resultSet.getString("password");
                User user = new User(id, name, password);
                list.add(user);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return list;
    }

    @Override
    public int selectTotalCount(UserQuery userQuery) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        int count = 0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select count(*) from banji where 1=1";
            //构造一个集合存放所有的条件
            List<Object> queryList = new ArrayList<>();
            String queryName = userQuery.getName();
            if (queryName != null && !queryName.equals("")) {
                sql += " and name like ?";
                queryList.add("%" + queryName + "%");
            }
            String queryAddress = userQuery.getPassword();
            if (queryAddress != null && !queryAddress.equals("")) {
                sql += " and password like ?";
                queryList.add("%" + queryAddress + "%");
            }
            statement = connection.prepareStatement(sql);
            for (int i = 0; i < queryList.size(); i++) {
                statement.setObject(i + 1, queryList.get(i));
            }
            System.out.println(statement);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                count = resultSet.getInt(1);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        };

        return count;
    }
}
